﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;

namespace JM.JCY.SharePointComponent.Committee.CommitteeDocument
{
    public partial class CommitteeDocumentUserControl : UserControl
    {
        public int _committeeId
        {
            set { ViewState["_committeeId"] = value; }
            get { return (int)ViewState["_committeeId"]; }
        }

        public int _userId
        {
            set { ViewState["_userId"] = value; }
            get { return (int)ViewState["_userId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (ApplicationContext.Current.GetKeyValue("userId") != null)
            {
                _userId = Int32.Parse(ApplicationContext.Current.GetKeyValue("userId").ToString());
            }
            if (!IsPostBack)
            {
                if (Request.QueryString["CommitteeId"] != null)
                {
                    _committeeId = Int32.Parse(Request.QueryString["CommitteeId"]);

                    using (UserManagementProxy userProxy = new UserManagementProxy())
                    {
                        CommitteeDataSet.T_CommitteeDataTable committeeDT = userProxy.GetCommitteeById(_committeeId);
                        if (committeeDT.Rows.Count > 0)
                            this.lblName.Text = committeeDT[0].CommitteeName;

                    }
                }
                else
                    _committeeId = 0;

            }
            if (_committeeId != 0)
            {
                if (IsMemberUser(_userId, _committeeId))
                {
                    SPWeb web = SPContext.Current.Web;
                    SPList docList = web.Lists[this.lblName.Text];
                    Microsoft.SharePoint.WebControls.ListView lv = new Microsoft.SharePoint.WebControls.ListView();
                    lv.ListId = docList.ID.ToString();
                    this.divCommittee.Controls.Add(lv);

                    string onclick = string.Format("javascript:NewItem2(event, '{0}');javascript:return false;", docList.DefaultNewFormUrl);
                    this.idHomePageNewItem.Attributes.Add("onclick", onclick);
                }
                else
                {
                    this.divCommitteeBack.Visible = false;
                }

            }

        }

        public bool IsMemberUser(int userId, int committeeId)
        {
            try
            {
                using (UserManagementProxy proxy = new UserManagementProxy())
                {
                    var dt = proxy.GetUserCommitteeMatch(userId, committeeId);
                    if (dt.Rows.Count > 0)
                        return true;
                    else
                        return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
